package com.slicktechnologies.server.addhocdownload;

import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.slicktechnologies.client.services.CsvService;
import com.slicktechnologies.shared.Contract;
import com.slicktechnologies.shared.Quotation;
import com.slicktechnologies.shared.ServiceType;
import com.slicktechnologies.shared.common.businessprocesslayer.Customer;
import com.slicktechnologies.shared.common.businessprocesslayer.Expense;
import com.slicktechnologies.shared.common.businessunitlayer.Company;
import com.slicktechnologies.shared.common.personlayer.Employee;

import static com.googlecode.objectify.ObjectifyService.ofy;
@SuppressWarnings("serial")
public class CsvWriter extends RemoteServiceServlet implements CsvService{
	
	public static ArrayList<Customer> custtable;
	public static ArrayList<Quotation> quotaion;
	public static ArrayList<Contract> contract;
	public static ArrayList<ServiceType> servicetype;
	public static ArrayList<Expense> expenseincome;
	
	public static ArrayList<Company>company;
	
	public static ArrayList<Employee> employees;


	private SimpleDateFormat fmt= new SimpleDateFormat("dd-MMM-yyyy");
	
	
	public CsvWriter()
	{
		
	}
	
/***************************CUSTOMER CSV***************************************/
	
	public void createCustomerHeader(PrintWriter writer)
	{
		//Font f=new Font("Times New Roman", Font.BOLD, 16);
		writer.append("ID");
		writer.append(",");
		writer.append("CUSTOMER NAME");
		writer.append(",");
		writer.append("CELL NUMBER");
		writer.append(",");
		writer.append("SALESPERSON");
		writer.append(",");
		writer.append("DATE");
		writer.append(",");
		writer.append("BRANCH");
		writer.append(",");
		writer.append("STATUS");
		writer.append(",");
		writer.append("\n");
	}
	
	
	public PrintWriter getCustomerCSV(PrintWriter writer) throws IOException
	{
		List<Customer> list=custtable;
		String name;
		createCustomerHeader(writer);
		for(Customer cust:list)
		{
			writer.append(cust.getCount()+"");
			writer.append(",");
			name=cust.getFullname();
			writer.append(name);
			writer.append(",");
			writer.append(cust.getCellNumber1()+"");
			writer.append(",");
			writer.append(cust.getEmployee());
			writer.append(",");
			writer.append(fmt.format(cust.getCreationDate()));
			writer.append(",");
			writer.append(cust.getBranch());
			writer.append(",");
			writer.append(cust.getStatus());
			writer.append("\n");	
		}
		return writer;
	}
	
	
	
	public void createAdvanceHeader(PrintWriter writer)
	{
		//Font f=new Font("Times New Roman", Font.BOLD, 16);
		writer.append("ID");
		writer.append(",");
		writer.append("EMPLOYEE NAME");
		writer.append(",");
		writer.append("CELL NUMBER");
		writer.append(",");
		writer.append("BRANCH");
		writer.append(",");
		writer.append("DEPARTMENT");
		writer.append(",");
		writer.append("EMPLOYEE TYPE");
		writer.append(",");
		
		writer.append("DESIGNATION");
		writer.append(",");
		writer.append("EMPLOYEE ROLE");
		writer.append(",");
		
		writer.append("ADVANCE ID");
		writer.append(",");
		writer.append("ADVANCE TYPE");
		writer.append(",");
		writer.append("REASON");
		writer.append(",");
		writer.append("PREVIOUS BALANCE");
		writer.append(",");

		writer.append("AMOUNT");
		writer.append(",");
		writer.append("ADVANCE FROM DATE");
		writer.append(",");
		writer.append("TO DATE");
		writer.append(",");
		writer.append("% INTEREST");
		writer.append(",");
		writer.append("DURATION");
		writer.append(",");
		writer.append("DEDUCTION AMOUNT");
		writer.append(",");
		writer.append("APPROVER NAME");
		writer.append(",");
		writer.append("STATUS");
		writer.append(",");
	    writer.append("\n");
		
	}
	
	
	public PrintWriter getQuotationCSV(PrintWriter writer) throws IOException
	{
		List<Quotation> quotlist=quotaion;
		
		createQuotationHeader(writer);
		writer.append("\n");
		for(Quotation q:quotlist)
		{
			writer.append(q.getCount()+"");
			writer.append(",");
			if(q.getContractCount()==-1)
				writer.append("N.A");
			else
				writer.append(q.getContractCount()+"");
			writer.append(",");
			
			if(q.getLeadCount()==-1)
				writer.append("N.A");
			else
				writer.append(q.getLeadCount()+"");
			writer.append(",");
			writer.append(q.getCustomerId()+"");
			writer.append(",");
			writer.append(q.getCinfo().getFullName());
			writer.append(",");
			writer.append(q.getCinfo().getCellNumber()+"");
			writer.append(",");
			writer.append(q.getEmployee());
			writer.append(","); 
			writer.append(fmt.format(q.getCreationDate())+"");
			writer.append(",");
			writer.append(q.getGroup());
			writer.append(","); 
			writer.append(q.getCategory());
			writer.append(","); 
			writer.append(q.getType());
			writer.append(","); 
			writer.append(q.getStatus());
			writer.append(",");
			writer.append(q.getBranch()); 
			 
			writer.append(",");
			 writer.append("\n");
			
		}
		
	   writer.append("\n");
			
		
		return writer;
	}
	
	/**********************************SERVICE CARD CSV****************************************/
	public void createServiceCardHeader(PrintWriter writer)
	{
		writer.append("ID");
		writer.append(",");
		
		
		writer.append("CUSTOMER ID");
		writer.append(",");
		writer.append("CUSTOMER NAME");
		writer.append(",");
		writer.append("CELL NUMBER");
		writer.append(",");
		writer.append("SALESPERSON");
		writer.append(",");
		writer.append("BRANCH");
		writer.append(",");
		writer.append("DATE");
		writer.append(",");
		writer.append("BALANCE");
		writer.append(",");
		writer.append("STATUS");
		writer.append(",");
		writer.append("\n");
	}
	
	public PrintWriter getServiceCardCSV(PrintWriter writer) throws IOException
	{
		List<Contract> quotlist=contract;
		createServiceCardHeader(writer);
		writer.append("\n");
		for(Contract q:quotlist)
		{
			
			writer.append(q.getCount()+"");
			writer.append(",");
		
			writer.append(q.getCustomerId()+"");
			writer.append(",");
			writer.append(q.getCustomerFullName());
			writer.append(",");
			writer.append(q.getCustomerCellNumber()+"");
			writer.append(",");
			writer.append(q.getEmployee());
			writer.append(",");
			writer.append(q.getBranch());
			writer.append(",");
			writer.append(fmt.format(q.getCreationDate())+"");
			writer.append(",");
			writer.append(q.getbalance()+"");
            writer.append(",");
			writer.append(q.getStatus());
			writer.append("\n");
			
		}
		return writer;
	}
	
	
	
	public void createQuotationHeader(PrintWriter writer)
	{
		
		writer.append("QUOTATION ID");
		writer.append(",");
		writer.append("CONTRACT ID");
		writer.append(",");
		writer.append("LEAD ID");
		writer.append(",");
		writer.append("CUSTOMER ID");
		writer.append(",");
		writer.append("CUSTOMER NAME");
		writer.append(",");
		writer.append("CELL NUMBER");
		writer.append(",");
		writer.append("SALESPERSON");
		writer.append(",");
		writer.append("DATE");
		writer.append(",");
		writer.append("GROUP");
		writer.append(",");
		writer.append("CATEGORY");
		writer.append(",");
		writer.append("TYPE");
		writer.append(",");
		writer.append("STATUS");
		writer.append(",");
		writer.append("BRANCH");
				
		
		
		
		
	}
	
	public PrintWriter getCompanyCSV(PrintWriter writer) throws IOException
	{
		createCompanyHeader(writer);
		for(Company l:company)
		{
			writer.append(l.getBusinessUnitName()+"");
			writer.append(",");
			writer.append(l.getPocName());
			writer.append(",");
			writer.append(l.getCellNumber1()+"");
			writer.append(",");
			writer.append(l.getNoOfUser()+"");
			writer.append(",");
			writer.append(fmt.format(l.getLicenseStartDate()));
			writer.append(",");
			writer.append(fmt.format(l.getLicenseEndDate()));
			writer.append(",");
			writer.append(l.getAccessUrl()+"");
			writer.append(",");
			writer.append(l.getStatus());
			writer.append(",");
			writer.append("\n");
		}
		return writer;
		
	}
	

	
	
	public void createserviceheader(PrintWriter writer)
	{
		writer.append("CONTRACT ID");
		writer.append(",");
		writer.append("NAME");
		writer.append(",");
		writer.append("PHONE");
		writer.append(",");
		writer.append("ADRESS");
		writer.append(",");
		writer.append("DATE");
		writer.append(",");
		writer.append("REASON FOR CHANGE");
		writer.append(",");
		writer.append("SERVICE ENGINEER");
		writer.append(",");
		writer.append("STATUS");
		writer.append("\n");
	}
	
	
	
		
	
	
	
	
	public void createCompanyHeader(PrintWriter writer)
	{
		writer.append("COMPANY NAME");
		writer.append(",");
		writer.append("POC NAME");
		writer.append(",");
		writer.append("CELL NUMBER");
		writer.append(",");
		writer.append("NUMBER OF USER");
		writer.append(",");
		writer.append("START DATE");
		writer.append(",");
		writer.append("END DATE");
		writer.append(",");
		writer.append("URL");
		writer.append(",");
		writer.append("STATUS");
		writer.append(",");
		writer.append("\n");
	}

	
	
	

	public void setcustomerlist(ArrayList<Customer> array)
	{
		custtable=array;
	}
	public void setquotationlist(ArrayList<Quotation> qarray)
	{
		quotaion=qarray;
	}
	
	
	public void setexpenselist(ArrayList<Expense> earray)
	{
		expenseincome=earray;
	}
	
	
	
	
	

	@Override
	public void setcontractlist(ArrayList<Contract> custarray) {
		contract=custarray;
		
	}

	@Override
	public void setCompanylist(ArrayList<Company> custarray) {
		this.company=custarray;
		
	}
	
	

	@Override
	public void setEmployeelist(ArrayList<Employee> custarray) {
		employees=custarray;
		
	}


	private void createEmployeeHeader(PrintWriter writer)
	{
		writer.append("COUNTRY");
		writer.append(",");
		writer.append("ID");
		writer.append(",");
		writer.append("NAME");
		writer.append(",");
		writer.append("PHONE");
		writer.append(",");
		writer.append("ROLE");
		writer.append(",");
		writer.append("REPORTS TO");
		writer.append(",");
		writer.append("BRANCH");
		writer.append(",");
		writer.append("DESIGNATION");
		writer.append(",");
		writer.append("DEPARTMENT");
		writer.append(",");
		writer.append("TYPE");
		writer.append(",");
		
		writer.append("ACCESS CARD NO.");
		writer.append(",");
		writer.append("ID CARD NO.");
		writer.append(",");
		
		writer.append("BANK NAME");
		writer.append(",");
		
		writer.append("BRANCH NAME");
		writer.append(",");
		
		writer.append("IIFC CODE");
		writer.append(",");
		
		writer.append("BANK ACCOUNT NUMBER");
		writer.append(",");
		writer.append("ESIC CARD NUMBER");
		writer.append(",");
		writer.append("PAN CARD NUMBER");
		writer.append(",");
		writer.append("PPF NUMBER");
		writer.append(",");
		
		writer.append("BLOOD GROUP");
		writer.append(",");
		writer.append("STATUS");
		writer.append(",");
		writer.append("\n");
	}
	
	public PrintWriter getEmployeeCSV(PrintWriter writer)
	{
		List<Employee> emplist=employees;
		createEmployeeHeader(writer);
		
		for(Employee emp:emplist)
		{
			writer.append(emp.getCountry()+"");
			writer.append(",");
			writer.append(emp.getCount()+"");
			writer.append(",");
			if(emp.getFullName()!=null)
			{
				writer.append(emp.getFullName());
				writer.append(",");
			}
			if(emp.getCellNumber1()!=null)
				writer.append(emp.getCellNumber1()+"");
				else
					writer.append("N.A");
				writer.append(",");
			
			if(emp.getRoleName()!=null)
				writer.append(emp.getRoleName());
			else
				writer.append("N.A");
			writer.append(",");
			
			if(emp.getReportsTo()!=null)
				writer.append(emp.getReportsTo());
			else
				writer.append("N.A");
			writer.append(",");
			
			if(emp.getBranchName()!=null)
				writer.append(emp.getBranchName());
			else
				writer.append("N.A");
			writer.append(",");
		
			if(emp.getDesignation()!=null)
				writer.append(emp.getDesignation());
			else
				writer.append("N.A");
			writer.append(",");
			
			if(emp.getDepartMent()!=null)
				writer.append(emp.getDepartMent());
			else
				writer.append("N.A");
			writer.append(",");
			
			if(emp.getEmployeeType()!=null)
				writer.append(emp.getEmployeeType());
			else
				writer.append("N.A");
			writer.append(",");
			
			
			if(emp.getAccessCardNo()!=null)
				writer.append(emp.getAccessCardNo());
			else
				writer.append("N.A");
			writer.append(",");
			if(emp.getIdCardNo()!=null)
				writer.append(emp.getIdCardNo());
			else
				writer.append("N.A");
			writer.append(",");
			
			if(emp.getEmployeeBankName()!=null)
				writer.append(emp.getBranchName());
			else
				writer.append("N.A");
			writer.append(",");
			
			if(emp.getIfscCode()!=null)
				writer.append(emp.getIfscCode());
			else
				writer.append("N.A");
			writer.append(",");
			
			if(emp.getEmployeeBankAccountNo()!=null)
				writer.append(emp.getEmployeeBankAccountNo());
			else
				writer.append("N.A");
			writer.append(",");
			
			if(emp.getEmployeeESICcode()!=null)
				writer.append(emp.getEmployeeESICcode());
			else
				writer.append("N.A");
			writer.append(",");
			
			if(emp.getEmployeePanNo()!=null)
				writer.append(emp.getEmployeePanNo());
			else
				writer.append("N.A");
			writer.append(",");
			
			if(emp.getPPFNaumber()!=null)
				writer.append(emp.getPPFNaumber());
			else
				writer.append("N.A");
			writer.append(",");
			
			
             
			if(emp.isStatus()==true)
				writer.append("Active");
			else
				writer.append("InActive");
			writer.append(",");
			
			if(emp.getBloodGroup()!=null)
				writer.append(emp.getBloodGroup());
			else
				writer.append("N.A");
			writer.append(",");
			
			writer.append("\n");
		}
		return writer;
	}


	
}
